package com.pig4cloud.pig.module.controller.mer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.MerSelfVipLevel;
import com.pig4cloud.pig.module.service.mer.IMerSelfVipLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商家自有会员等级Controller
 *
 * @author ruoyi
 * @date 2023-05-28
 */
@Validated
@Api(value = "商家自有会员等级控制器", tags = {"商家自有会员等级管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mer/self/vip/level")
public class MerSelfVipLevelController {

    private final IMerSelfVipLevelService merSelfVipLevelService;

    /**
     * 查询商家自有会员等级列表
     */
    @ApiOperation("查询商家自有会员等级列表")
    @PreAuthorize("@pms.hasPermission('module:merSelfVipLevel:list')")
    @GetMapping("/list")
    public TableDataInfo<MerSelfVipLevel> list(@Validated MerSelfVipLevel merSelfVipLevel) {
        QueryWrapper<MerSelfVipLevel> qw = new QueryWrapper<>();

        TableDataInfo<MerSelfVipLevel> td = PageUtils.buildDataInfo(merSelfVipLevelService.page(PageUtils.buildPage(),qw));
        List<MerSelfVipLevel> list = td.getData();
        return td;
    }

    /**
     * 获取商家自有会员等级详细信息
     */
    @ApiOperation("获取商家自有会员等级详细信息")
    @PreAuthorize("@pms.hasPermission('module:merSelfVipLevel:query')")
    @GetMapping("/{id}")
    public R<MerSelfVipLevel> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MerSelfVipLevel merSelfVipLevel = merSelfVipLevelService.getById(id);
        return R.ok(merSelfVipLevel);
    }

    /**
     * 新增商家自有会员等级
     */
    @ApiOperation("新增商家自有会员等级")
    @PreAuthorize("@pms.hasPermission('module:merSelfVipLevel:add')")
    @SysLog("商家自有会员等级")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MerSelfVipLevel merSelfVipLevel) {
        return R.okByRow(merSelfVipLevelService.save(merSelfVipLevel) ? 1 : 0);
    }

    /**
     * 修改商家自有会员等级
     */
    @ApiOperation("修改商家自有会员等级")
    @PreAuthorize("@pms.hasPermission('module:merSelfVipLevel:edit')")
    @SysLog("商家自有会员等级")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MerSelfVipLevel merSelfVipLevel) {
        return R.okByRow(merSelfVipLevelService.updateById(merSelfVipLevel) ? 1 : 0);
    }

    /**
     * 删除商家自有会员等级
     */
    @ApiOperation("删除商家自有会员等级")
    @PreAuthorize("@pms.hasPermission('module:merSelfVipLevel:remove')")
    @SysLog("商家自有会员等级")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(merSelfVipLevelService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
